package com.huoyun.tag;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.huoyun.base.bean.Region;
import com.huoyun.base.service.NodeInfoService;
import com.huoyun.base.service.RegionService;
import com.opensymphony.xwork2.util.ValueStack;

public class RegionSelectTag extends AbstractUITag {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6650604228934921870L;

	private NodeInfoService nodeInfoService;
	
	/**
	 * 省份选择框的名称
	 */
	private String provinceName;
	
	/**
	 * 省份的默认值
	 */
	private String provinceValue;
	
	/**
	 * 城市选择框的名称
	 */
	private String cityName;
	
	/**
	 * 城市的默认值
	 */
	private String cityValue;
	
	/**
	 * 城镇选择框的名称
	 */
	private String townName;
	
	/**
	 * 城镇的默认值
	 */
	private String townValue;
	
	/**
	 * 类型：view表示直接展示省市区名称，没有下拉选择框，select表示下拉选择框显示，默认为下拉选择框
	 */
	private String type;
	
	@Override
	public Component getBean(ValueStack valueStack, HttpServletRequest request,
			HttpServletResponse response) 
	{
		return new RegionSelectBean(valueStack, request, response);
	}
	
	@Override  
    protected void populateParams() {  
        super.populateParams();  
        RegionSelectBean regionSelectBean = (RegionSelectBean) component;  
        regionSelectBean.setProvinceName(provinceName);
        regionSelectBean.setCityName(cityName);
        regionSelectBean.setTownName(townName);
        
        if(StringUtils.isNotEmpty(provinceName))
        {
        	regionSelectBean.setProvinceId(provinceName.replace(".", ""));
        }
        
        if(StringUtils.isNotEmpty(cityName))
        {
        	regionSelectBean.setCityId(cityName.replace(".", ""));
        }
        
        if(StringUtils.isNotEmpty(townName))
        {
        	regionSelectBean.setTownId(townName.replace(".", ""));
        }
        
        regionSelectBean.setType(type);
        
        //如果只是查看相应的节点信息
        if("view".equals(type))
        {
        	if(StringUtils.isNotEmpty(provinceValue))
            {
        		Region pRegion = getRegionByCode(provinceValue);
        		if (pRegion != null)
        		{
        			regionSelectBean.setProvinceValue(pRegion.getName());
        		}
            }
        	
        	if(StringUtils.isNotEmpty(cityValue))
            {
        		Region cRegion = getRegionByCode(cityValue);
        		if (cRegion != null)
        		{
        	    	regionSelectBean.setCityValue(cRegion.getName());
        		}
            }
        	
        	if(StringUtils.isNotEmpty(townValue))
            {
        		Region tRegion = getRegionByCode(townValue);
        		if (tRegion != null)
        		{
        			regionSelectBean.setTownValue(tRegion.getName());
        		}
            }
        }
        else
        {
        	regionSelectBean.setProvinces(getAllProvinces());
            
            if(StringUtils.isNotEmpty(provinceValue))
            {
            	regionSelectBean.setProvinceValue(provinceValue);
            	
            	regionSelectBean.setCitys(getAllCitys(provinceValue));
            }
            
            if(StringUtils.isNotEmpty(cityValue))
            {
            	regionSelectBean.setCityValue(cityValue);
            	
            	regionSelectBean.setTowns(getAllTowns(cityValue));
            }
            
            if(StringUtils.isNotEmpty(townValue))
            {
            	regionSelectBean.setTownValue(townValue);
            }
        }	
    }

	private List<Region> getAllProvinces()
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		RegionService regionService = (RegionService)wac.getBean("regionService");
		
		return regionService.queryChilds(1);
	}
	
	private List<Region> getAllCitys(String provinceCode)
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		RegionService regionService = (RegionService)wac.getBean("regionService");
		
		return regionService.queryChilds(provinceCode);
	}
	
	private List<Region> getAllTowns(String cityCode)
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		RegionService regionService = (RegionService)wac.getBean("regionService");
		
		return regionService.queryChilds(cityCode);
	}
	
	private Region getRegionByCode(String code)
	{
		WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
		RegionService regionService = (RegionService)wac.getBean("regionService");
		
		return regionService.getRegionByCode(code);
	}

	public String getProvinceName() {
		return provinceName;
	}

	public void setProvinceName(String provinceName) {
		this.provinceName = provinceName;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public String getTownName() {
		return townName;
	}

	public void setTownName(String townName) {
		this.townName = townName;
	}

	public NodeInfoService getNodeInfoService() {
		return nodeInfoService;
	}

	public void setNodeInfoService(NodeInfoService nodeInfoService) {
		this.nodeInfoService = nodeInfoService;
	}

	public String getProvinceValue() {
		return provinceValue;
	}

	public void setProvinceValue(String provinceValue) {
		this.provinceValue = provinceValue;
	}

	public String getCityValue() {
		return cityValue;
	}

	public void setCityValue(String cityValue) {
		this.cityValue = cityValue;
	}

	public String getTownValue() {
		return townValue;
	}

	public void setTownValue(String townValue) {
		this.townValue = townValue;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

}
